//
//  TechnicalViewController.m
//  PrintPort
//
//  Created by 华谕 on 2017/11/11.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "TechnicalViewController.h"
#import "PaymentChannelsViewController.h"
#import "DeliveryRequireViewController.h"
#import "GoodsDetailsViewController.h"
#import "ImageModel.h"

static NSString *cellPath = @"TechnicalCell";
@interface TechnicalViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)NSArray *modelAry;
@property (nonatomic, strong)NSArray *imageAry;
@property (nonatomic, strong)NSArray *titleAry;
@property (nonatomic, strong)NSString *payImg;
@property (nonatomic, strong)NSString *pushImg;
@property (nonatomic, strong)NSString *advImg;

@end

@implementation TechnicalViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestProductType];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _imageAry = @[@"dan",@"la",@"kuang",@"gang",@"you"];
    _titleAry = @[@"单拉网",@"拉晒网",@"自带框晒网",@"晒钢板",@"调油"];
    _payImg = @"";
    _pushImg = @"";
    _advImg = @"";
    [self creatUI];
}

- (void)creatUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"技术大厅";
    CGFloat heightX = 0;
    if (SCREEN_HEIGHT == 812)
    {
        heightX = 34;
    }
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-TABBAR_HEIGHT-heightX) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tableview];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0)
    {
        if (_modelAry.count > 0)
        {
            for (int i = 0; i < _modelAry.count; i++)
            {
                ImageModel *model = _modelAry[i];
                UIImageView *imageview = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH/10*(2*i+1)-22.5*displaywidth, 15*displaywidth, 45*displaywidth, 45*displaywidth) ImageName:_imageAry[i]];
                [imageview sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:[UIImage imageNamed:_imageAry[i]]];
                [cell.contentView addSubview:imageview];
                UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH/5*i, 65*displaywidth, SCREEN_WIDTH/5, 30*displaywidth) Font:12.0 Text:model.typeName Color:RGB(77, 77, 77)];
                nameLab.textAlignment = NSTextAlignmentCenter;
                nameLab.lineBreakMode = NSLineBreakByTruncatingTail;
                [cell.contentView addSubview:nameLab];
                UIButton *btn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH/5*i, 0, SCREEN_WIDTH/5, 100*displaywidth) ImageName:nil Target:self Action:@selector(closeItemAction:) Title:nil Font:12.0];
                btn.backgroundColor = [UIColor clearColor];
                btn.tag = 100+i;
                [cell.contentView addSubview:btn];
                
            }
        }
        
    }
    else if (indexPath.row == 1)
    {
        UIImageView *leftimageview = [ZCControl createImageViewWithFrame:CGRectMake(0, 10*displaywidth, SCREEN_WIDTH/2-3*displaywidth, 120*displaywidth) ImageName:@"zhifutongdao"];
        if (![_payImg isEqualToString:@""])
        {
            [leftimageview sd_setImageWithURL:[NSURL URLWithString:_payImg] placeholderImage:[UIImage imageNamed:@"zhifutongdao"]];
        }
        leftimageview.contentMode = UIViewContentModeScaleAspectFill;
        leftimageview.layer.masksToBounds = YES;
        [cell.contentView addSubview:leftimageview];
        UIImageView *rightview = [ZCControl createImageViewWithFrame:CGRectMake(SCREEN_WIDTH/2+3*displaywidth, 10*displaywidth, SCREEN_WIDTH/2-3*displaywidth, 120*displaywidth) ImageName:@"disongxuqiu"];
        if (![_pushImg isEqualToString:@""])
        {
            [rightview sd_setImageWithURL:[NSURL URLWithString:_pushImg] placeholderImage:[UIImage imageNamed:@"disongxuqiu"]];
        }
        rightview.contentMode = UIViewContentModeScaleAspectFill;
        rightview.layer.masksToBounds = YES;
        [cell.contentView addSubview:rightview];
        UIButton *leftbtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2-3*displaywidth, 120*displaywidth) ImageName:nil Target:self Action:@selector(leftAction) Title:nil Font:12.0];
        leftbtn.backgroundColor = [UIColor clearColor];
        [leftimageview addSubview:leftbtn];
        UIButton *rightbtn = [ZCControl createButtonWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/2-3*displaywidth, 120*displaywidth) ImageName:nil Target:self Action:@selector(rightAction) Title:nil Font:12.0];
        rightbtn.backgroundColor = [UIColor clearColor];
        [rightview addSubview:rightbtn];
    }
    else
    {
        UIImageView *ggimageview = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120*displaywidth) ImageName:@"jishugg"];
        if (![_advImg isEqualToString:@""])
        {
            [ggimageview sd_setImageWithURL:[NSURL URLWithString:_advImg] placeholderImage:[UIImage imageNamed:@"jishugg"]];
        }
        ggimageview.contentMode = UIViewContentModeScaleAspectFill;
        ggimageview.layer.masksToBounds = YES;
        [cell.contentView addSubview:ggimageview];
    }
    return cell;
  
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 100*displaywidth;
    }
    else if (indexPath.row == 1)
    {
        return 140*displaywidth;
    }
    
    return 120*displaywidth;
}


- (void)closeItemAction:(UIButton *)btn
{
    if (_modelAry.count > 0)
    {
        NSInteger index = btn.tag-100;
        ImageModel *model = _modelAry[index];
        GoodsDetailsViewController *vc = [[GoodsDetailsViewController alloc] init];
        vc.idStr = model.ID;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        [self requestProductType];
    }
}

//支付通道
- (void)leftAction
{ 
    PaymentChannelsViewController *vc = [[PaymentChannelsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

//递送需求
- (void)rightAction
{
    DeliveryRequireViewController *vc = [[DeliveryRequireViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestProductType
{
    WEAKSELF;
    [HYBaseHandler requestTechnologyHomeParmers:nil Success:^(id obj)
    {
        if ([obj server_Getcodecheck])
        {
            _modelAry = [ImageModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"productTypeList"]];
            _payImg = obj[@"data"][@"paymentChannelImg"];
            _pushImg = obj[@"data"][@"deliveryDemandImg"];
            _advImg = obj[@"data"][@"horizontalAD"];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
        
    } failed:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
